import Vue from 'vue'
import VueI18n from 'vue-i18n'
// import enLang from 'element-ui/lib/locale/lang/en'
// import zhLang from 'element-ui/lib/locale/lang/zh-CN'
// import ElementLocale from 'element-ui/lib/locale'
import en from '@/assets/lang/en'
import zh from '@/assets/lang/zh'
import tw from '@/assets/lang/tw'

Vue.use(VueI18n)

function getLanguage () {
  var lang = 'en'
  var langList = ['zh', 'en', 'tw']
  var storeLang = window.localStorage.getItem('lang')
  if (storeLang && langList.indexOf(storeLang) >= 0) {
    return storeLang
  }
  if (navigator.language) {
    var tempLang = navigator.language
    if (tempLang.indexOf('zh-CN') >= 0) {
      lang = 'zh'
    } else if (tempLang.indexOf('TW') >= 0 || tempLang.indexOf('tw') >= 0) {
      lang = 'tw'
    }
  }
  console.log('i18n: ', lang)
  return lang
}

const messages = {
  en: {
    ...en
  },
  zh: {
    ...zh
  },
  tw: {
    ...tw
  }
}

const i18n = new VueI18n({
  local: getLanguage(),
  messages,
  silentTranslationWarn: true,
  fallbackLocale: getLanguage()
})

// ElementLocale.i18n((key, value) => i18n.t(key, value))

export default i18n
